# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

pkgbase="sqlite"
pkgname=('sqlite' 'libsqlite' 'libsqlite-devel' 'sqlite-doc')
_amalgamationver=3080802
_docver=${_amalgamationver}
pkgver=3.8.8.2
pkgrel=1
pkgdesc="A C library that implements an SQL database engine"
arch=('i686' 'x86_64')
license=(PublicDomain)
url="http://www.sqlite.org/"
depends=('libreadline' 'icu' 'zlib')
makedepends=('libreadline-devel' 'icu-devel' 'zlib-devel')
source=( # tarball containing the amalgamation for SQLite >= 3.7.5 together with a configure script and makefile for building it; includes now also the Tcl Extension Architecture (TEA)
        http://www.sqlite.org/2015/sqlite-autoconf-$_amalgamationver.tar.gz
        http://www.sqlite.org/2015/sqlite-doc-${_docver}.zip
        LICENSE
        'sqlite-3.8.0.1-msys2.patch'
        'sqlite3-3.8.8.2-1.src.patch'
        'sqlite3-cygwin-msys.patch')
options=('!libtool' '!emptydirs' '!debug' 'strip')
sha1sums=('1db237523419af7110e1d92c6b766e965f9322e4'
          'a11a6ea95d3d4a88b8d7d4e0cb6fcc3e5f4bf887'
          'e2aa07adae13aed713860b74165d3325a18c6792'
          'f357145f11da54b3dfd9c45e594d93960af87105'
          '10bb572e806e93fe9bfd2e4c024415778880180c'
          '69b3f261aae4990d23daa14ca0d2f0c8b53fb423')

prepare() {
  cd "$srcdir"/sqlite-autoconf-$_amalgamationver

  patch -p2 -i ${srcdir}/sqlite3-3.8.8.2-1.src.patch
  patch -p1 -i ${srcdir}/sqlite3-cygwin-msys.patch
  patch -p1 -i ${srcdir}/sqlite-3.8.0.1-msys2.patch

  autoreconf -fi
}

build() {
  export CPPFLAGS="$CPPFLAGS \
    -DHAVE_MALLOC_H \
    -DHAVE_MALLOC_USABLE_SIZE \
    -DSQLITE_ENABLE_COLUMN_METADATA=1 \
    -DSQLITE_ENABLE_FTS4=1 \
    -DSQLITE_ENABLE_RTREE=1 \
    -DSQLITE_ENABLE_SQLLOG=1 \
    -DSQLITE_ENABLE_STAT4=1 \
    -DSQLITE_ENABLE_UNLOCK_NOTIFY=1 \
    -DSQLITE_MAX_MMAP_SIZE=0x7fff0000 \
    -DSQLITE_MAX_PATH_BYTES=4096 \
    -DSQLITE_OS_UNIX=1 \
    -DSQLITE_OS_WIN=1 \
    -DSQLITE_SOUNDEX=1 \
    -DSQLITE_USE_FCNTL_TRACE=1 \
    -DSQLITE_WIN32_NO_ANSI=1 \
    -DSQLITE_WIN32_MAX_PATH_BYTES=4096 \
    -DUSE_SYSTEM_SQLITE=1"

  cd "$srcdir"/sqlite-autoconf-$_amalgamationver
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --build=${CHOST} \
    --enable-shared \
    --enable-static

  make
  make DESTDIR=${srcdir}/dest install
}

_install_license() {
  install -D -m644 ${srcdir}/LICENSE ${pkgdir}/usr/share/licenses/${pkgname}/LICENSE
}

package_sqlite() {
  pkgdesc="A C library that implements an SQL database engine"
  depends=('libreadline' 'libsqlite')
  provides=("sqlite3=$pkgver")
  replaces=("sqlite3")

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
  _install_license
}

package_libsqlite() {
  pkgdesc="Sqlite3 library"
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
  _install_license
}

package_libsqlite-devel() {
  pkgdesc="Sqlite3 headers and libraries"
  groups=('development')
  depends=("libsqlite=${pkgver}")
  options+=('staticlibs')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
  _install_license
}

package_sqlite-doc() {
  pkgdesc="Most of the static HTML files that comprise this website, including all of the SQL Syntax and the C/C++ interface specs and other miscellaneous documentation"
  #arch=('any') - not yet supported
  provides=("sqlite3-doc=$pkgver")
  replaces=("sqlite3-doc")

  #cd ${srcdir}/sqlite-doc-${_amalgamationver}
  cd ${srcdir}/sqlite-doc-${_docver}
  mkdir -p ${pkgdir}/usr/share/doc/${pkgbase}
  cp -R *  ${pkgdir}/usr/share/doc/${pkgbase}/

  # fix permissions and remove obsolete files; https://bugs.archlinux.org/task/24605
  find ${pkgdir} -type f -perm 755 -exec ls -lha {} \;
  find ${pkgdir} -type f -perm 755 -exec chmod 644 {} \;

  find ${pkgdir} -type f -name '*~' -exec ls -lha {} \;
  find ${pkgdir} -type d -name '*~' -exec ls -lha {} \;
  find ${pkgdir} -name '*~' -exec rm -f {} \;

  find ${pkgdir} -type f -name '.~*' -exec ls -lha {} \; # /build/pkg/sqlite-doc/usr/share/doc/sqlite/images/fileformat/.~lock.indexpage.odg#
  find ${pkgdir} -type d -name '.~*' -exec ls -lha {} \;
  find ${pkgdir} -name '.~*' -exec rm -f {} \;

  _install_license
}
